Skip to content

feat(mcp): record input_required rounds as $mcp_input_required - #1305

Open
gesh wants to merge 2 commits into
mainfrom
georgis/mcp-input-required
Open

gesh wants to merge 2 commits into
mainfrom
georgis/mcp-input-required

Conversation

@gesh

@gesh gesh commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

💡 Motivation and Context

  • A tool that asks for confirmation before it runs counts as two successful calls on Python MCP servers.
  • A client on an earlier revision gets an error for that round, which still counts as a success.

Changes

  • instrument() sends $mcp_input_required for each round, as the spec defines and Go and Rust do. It carries the tool name, duration and request methods only.
  • For custom dispatchers, prepare_tool_result() returns the round's input_request_methods, and PostHogMCP.capture_input_required() records them.
  • The change sits in ToolCallLifecycle.record_result, which every adapter calls. mcp 1.x servers see no change.
One confirmed call Before After
Round that asks $mcp_tool_call $mcp_input_required
Retry $mcp_tool_call $mcp_tool_call
Round, legacy client $mcp_tool_call, success $mcp_tool_call, input_required error
Example event and custom dispatcher
{
  "event": "$mcp_input_required",
  "properties": {
    "$mcp_tool_name": "delete_project",
    "$mcp_duration_ms": 3.2,
    "$mcp_input_request_methods": ["elicitation/create"],
    "$mcp_protocol_version": "2026-07-28",
    "$session_id": "ses_...",
    "$mcp_source": "posthog_mcp_analytics"
  }
}
call = posthog.prepare_tool_call(name, raw_args, original_tool=tool)
prepared = posthog.prepare_tool_result(dispatch(name, call.args), call)
if prepared.input_request_methods is not None:
    posthog.capture_input_required(
        name,
        input_request_methods=prepared.input_request_methods,
        duration_ms=duration_ms,
        session_id=prepared.session_id,
        conversation_id=prepared.conversation_id,
    )
else:
    posthog.capture_tool_call(
        name,
        duration_ms=duration_ms,
        session_id=prepared.session_id,
        conversation_id=prepared.conversation_id,
    )
return prepared.result

💚 How did you test it?

  • New tests cover modern and legacy wire calls, raw servers, and the manual API.
  • Not covered: resources/read and prompts/get rounds. Not run against a real MCP client.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code with Claude Opus 5.5 (claude-opus-5-5). Skills: debugging-mcp-analytics, writing-pr-descriptions.
More context for AI agents
  • Follow-ups: the same change in posthog-js and posthog-ruby, and the event in the posthog.com events.mdx table.
  • mcp 2.x has no shim that fulfils a round for a legacy client. It rejects the result, so the legacy case is a failed call, as the spec says for a sequence the framework ends.
  • Disabling the detector in record_result turns the 4 automatic-instrumentation tests red.

🤖 Generated with Claude Code

A tool on the 2026-07-28 revision that asks for more input (for example a
confirmation form) answers tools/call with an InputRequiredResult and runs
on the client's retry. The SDK counted the round and the retry as two
successful $mcp_tool_call events.

- instrument(): a round sends $mcp_input_required with $mcp_tool_name,
  $mcp_duration_ms and $mcp_input_request_methods. Only the retry that
  completes the call is a $mcp_tool_call. The requests, answers and
  requestState are never captured.
- A round returned to a client on an earlier revision, which mcp answers
  with an error, is a failed $mcp_tool_call with $mcp_error_type
  "input_required" instead of a successful one.
- PostHogMCP.capture_input_required() for custom dispatchers.
- The branch lives in ToolCallLifecycle.record_result, so every adapter
  gets it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gesh gesh self-assigned this Nov 25, 2026
A custom dispatcher had to check the result type itself and hardcode the
methods it passed to capture_input_required(). prepare_tool_result() now
sets PreparedToolResult.input_request_methods when the result is an
input_required round, ordered by key, and leaves it None otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 133.67 KiB/op 133.67 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,345.53 count/op 1,345.53 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 136.91 µs/op 136.43 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 37.77 MiB 37.71 MiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 40.22 MiB 40.16 MiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 40.22 MiB 40.16 MiB 0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 136.12 µs/op 135.97 µs/op 0.1% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 40.82 MiB 40.82 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 185.76 KiB/op 185.76 KiB/op No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,337.57 count/op 1,337.57 count/op No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 177.82 µs/op 171.61 µs/op 3.5% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.26 MiB 41.38 MiB 0.3% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 47.04 MiB 47.16 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 47.04 MiB 47.16 MiB 0.2% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 171.71 µs/op 163.82 µs/op 4.6% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.55 MiB 51.57 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 138.74 KiB/op 138.74 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,390.42 count/op 1,390.42 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 151.61 µs/op 157.93 µs/op 4.2% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 37.66 MiB 37.71 MiB 0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 37.84 MiB 37.87 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 37.84 MiB 37.87 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 149.11 µs/op 157.02 µs/op 5.3% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 40.82 MiB 40.82 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 190.92 KiB/op 190.92 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,382.39 count/op 1,382.36 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 175.75 µs/op 179.34 µs/op 2% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 41.19 MiB 41.12 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 41.65 MiB 41.57 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 41.25 MiB 41.17 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 167.06 µs/op 171.44 µs/op 2.6% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.54 MiB 41.5 MiB 0.1% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 113.38 µs/op 117.42 µs/op 3.6% more time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 113.38 µs/op 118.55 µs/op 4.6% more time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 114.82 µs/op 109.42 µs/op 4.7% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 116.08 µs/op 110.28 µs/op 5% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 225.43 KiB/op 225.43 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,279.42 count/op 2,279.42 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 82.63 µs/op 83.22 µs/op 0.7% more time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 82.63 µs/op 83.23 µs/op 0.7% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,413.69 count/op 22,413.69 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 770.44 µs/op 771.43 µs/op 0.1% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 770.45 µs/op 771.43 µs/op 0.1% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.63 MiB/op 21.63 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 223,655.44 count/op 223,655.44 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.69 ms/op 7.65 ms/op 0.5% less time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.69 ms/op 7.65 ms/op 0.5% less time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 43.97 MiB 43.65 MiB 0.7% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 44.27 MiB 43.93 MiB 0.8% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 44.27 MiB 43.93 MiB 0.8% less memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43.96 MiB 44.04 MiB 0.2% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 43.06 MiB 43 MiB 0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 43.07 MiB 43.01 MiB 0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 43.07 MiB 43.01 MiB 0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.97 MiB 42.72 MiB 0.6% less memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 15.74 KiB/op 15.74 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 162.77 count/op 162.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 10.05 µs/op 9.84 µs/op 2.1% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 10.05 µs/op 9.84 µs/op 2.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 24.6 KiB/op 24.6 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 239.28 count/op 239.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 13.26 µs/op 13.33 µs/op 0.6% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 13.26 µs/op 13.33 µs/op 0.6% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 39.81 KiB/op 39.81 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 388.27 count/op 388.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 19.07 µs/op 18.96 µs/op 0.6% less time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 19.07 µs/op 18.96 µs/op 0.6% less time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 60.59 KiB/op 60.59 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 584.27 count/op 584.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 27.09 µs/op 27.26 µs/op 0.6% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 27.09 µs/op 27.26 µs/op 0.6% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 59.22 KiB/op 59.22 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 567.77 count/op 567.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 25.92 µs/op 25.83 µs/op 0.3% less time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 25.92 µs/op 25.83 µs/op 0.3% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.04 KiB/op 19.04 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 192.27 count/op 192.27 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 10.96 µs/op 10.9 µs/op 0.5% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 10.96 µs/op 10.9 µs/op 0.5% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 20.66 KiB/op 20.66 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 197.77 count/op 197.77 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 11.79 µs/op 11.7 µs/op 0.8% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 11.79 µs/op 11.7 µs/op 0.8% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 14.89 KiB/op 14.89 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 152.77 count/op 152.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 9.31 µs/op 9.3 µs/op <0.1% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 9.31 µs/op 9.3 µs/op <0.1% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 15.9 KiB/op 15.9 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 166.77 count/op 166.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 10.21 µs/op 10.2 µs/op 0.1% less time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 10.21 µs/op 10.2 µs/op 0.1% less time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared eeb6d675da3b8977177bd71805706b13a6d65da8 → 712e69c75a9b80003142ff465e3db3a0d91b91cf.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@gesh
gesh marked this pull request as ready for review November 25, 2026 14:24
@gesh
gesh requested a review from a team as a code owner November 25, 2026 14:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant